package heven.holt.wan.android.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import heven.holt.wan.android.extensions.requestCoroutine
import heven.holt.wan.android.model.ProjectTreeModel
import heven.holt.wan.android.repository.WanProjectRepository
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ProjectViewModel @Inject constructor(
    private val wanProjectRepository: WanProjectRepository
) : ViewModel() {
    val projectTreeLiveData: MutableLiveData<List<ProjectTreeModel>> = MutableLiveData()

    fun requestProjectTree() {
        viewModelScope.launch {
            val projectTree =
                requestCoroutine { wanProjectRepository.getProjectTree() } ?: return@launch
            projectTreeLiveData.postValue(projectTree)
        }
    }
}